#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-08-30 12:27
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：

from sqlalchemy import Column, DateTime, FLOAT, Integer, NVARCHAR, String,REAL,DECIMAL
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func

base = declarative_base()


# 获取操作票中间主表
def get_ticket_main(name, _type = 'CY'):
    class table_main(base):
        __tablename__ = "T_{}_{}CYCZP_MASTE".format(540 if _type == "CY" else 550, name)  # 表名
        F_ID = Column(String(36), comment = '主键', primary_key = True)  # 主键
        F_CZPBH = Column(String(20), comment = '票编号', index = True)  # 票编号
        F_CZPLX = Column(String(20), comment = '票类型')  # 票类型
        F_CZPZT = Column(String(20), comment = '票状态')  # 票状态
        F_CZPJG = Column(String(20), comment = '票结果')  # 票结果
        F_CZPSCMS = Column(String(20), comment = '票生成模式')  # 票生成模式
        F_CZPZXMS = Column(String(20), comment = '票执行模式')  # 票执行模式
        F_KPRY = Column(String(20), comment = '开票人员')  # 开票人员
        F_KPSJ = Column(DateTime(), comment = '开票时间')  # 开票时间
        F_CZPKSSJ = Column(DateTime(), comment = '开始时间')  # 开始时间
        F_CZPJSSJ = Column(DateTime(), comment = '结束时间')  # 结束时间
        F_CZRY = Column(String(20), comment = '操作人员')  # 操作人员
        F_YCXX = Column(String(1000), comment = '异常信息')  # 异常信息
        F_CSSX = Column(Integer(), comment = '超时时限')  # 超时时限
        F_PCH = Column(Integer(), comment = '批次号')  # 批次号
        F_CYBH = Column(String(20), comment = '采样码')  # 采样码
        F_MEIZHONG = Column(Integer(), comment = '煤种')  # 煤种
        # 采样
        if _type == "CY":
            F_CYFS = Column(String(20), comment = '采样方式')  # 采样方式
            F_YJLML = Column(FLOAT(20), comment = '预计来煤量')  # 预计来煤量
            F_YJLMCS = Column(Integer(), comment = '预计来煤车数')  # 预计来煤车数
            F_SFB = Column(String(20), comment = '缩分比')  # 缩分比
            F_CYPL = Column(String(20), comment = '采样批量')  # 采样批量
            F_FCJSBH = Column(String(20), comment = '翻车机识别编号')  # 翻车机识别编号
            F_TYBMHJ = Column(String(2000), comment = '样桶编码合集')  # 样桶编码合集
            F_CCZQ = Column(Integer(), comment = '初采周期')  # 初采周期
            F_CICZQ = Column(Integer(), comment = '次采周期')  # 次采周期
            F_CCCS = Column(Integer(), comment = '次采次数')  # 次采次数
            F_YANGZHONG = Column(FLOAT(20), comment = '样重')  # 样重
            # F_SFLDBY = Column(String(20), comment = '煤种')                                          #
            F_GMJZT = Column(String(20), comment = '给煤机状态')  # 给煤机状态
            F_GMJPL = Column(Integer(), comment = '给煤机批量')  # 给煤机批量
        # 制样
        else:
            F_ZYBH = Column(String(20), comment = '制样编号')  # 制样编号
            F_FYTH = Column(String(20), comment = '封样桶号')  # 封样桶号
            F_BZM = Column(String(20), comment = '包装码')  # 包装码
            F_BZSL = Column(Integer(), comment = '包装数量')  # 包装数量
            F_BZYPML = Column(FLOAT(20), comment = '包装样器煤量')  # 包装样器煤量
            F_CSCSSX = Column(Integer(), comment = '传输超时时限')  # 传输超时时限
            F_SHIDU = Column(String(20), comment = '湿度')  # 湿度
            F_MTLD = Column(String(20), comment = '煤炭粒径')  # 煤炭粒径

        F_ZDSBBH = Column(Integer())  # 终端设备号
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return table_main


# 获取操作票子表
def get_ticket_sub(name = '', _type = 'CY', index = 1):
    sub = {0: "ZERO ", 1: "ONE", 2: "TWO", 3: "THREE", }

    # _={4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",9: "NINE", 10: "TEN", 11: "ELEVEN", 12: "TWELVE", 13: "THIRTEEN", 14: "FOURTEEN", 15: "FIFTEEN",16: "SIXTEEN", 17: "SEVENTEEN", 18: "EIGHTEEN", 19: "NINETEEN"}
    class table_sub(base):
        __tablename__ = "T_{}_{}CYCZP_DETAIL{}".format(540 if _type == "CY" else 550, name if _type == "CY" else "",
                                                       sub[index])  # 表名
        F_ID = Column(String(36), comment = '主键', primary_key = True)  # ID
        F_PID = Column(String(36), comment = '主表主键')  # PID
        F_XUHAO = Column(Integer(), comment = '序号', server_default = '0')  # 顺序号
        F_ZHUANGTAI = Column(String(8), comment = '状态')  # 状态
        F_YCXX = Column(String(2000), comment = '异常信息')  # 异常信息
        # 采样
        if index == 1 and _type == "CY":
            F_CHEHAO = Column(NVARCHAR(20), comment = '车号')  # 车号
            F_PIAOZHONG = Column(FLOAT(18, 3), comment = '票重')  # 票重
            F_CYJ = Column(Integer(), comment = '采样机')  # 采样机
            F_CYKSSJ = Column(DateTime(), comment = '采样开始时间')  # 采样开始时间
            F_CYJSSJ = Column(DateTime(), comment = '采样结束时间')  # 采样结束时间
            F_CYDS = Column(Integer(), comment = '采样点数')  # 采样点数
            F_CYDH = Column(String(100), comment = '采样点号')  # 采样点号
        # 制样
        elif index == 1 and _type == "ZY":
            F_PINGMA = Column(String(20), comment = '瓶码')  # 瓶码
            F_YPRFIDBM = Column(String(20), comment = '样瓶RFID编码')  # 样瓶RFID编码
            F_PDRFIDBM = Column(String(20), comment = '瓶底RFID码')  # 瓶底RFID码
            F_YPLX = Column(String(8), comment = '样品类型')  # 样品类型
            F_WCSJ = Column(DateTime(), comment = '完成时间')  # 完成时间
            F_KLD = Column(String(20), comment = '颗粒度')  # 颗粒度
            F_FENSHU = Column(Integer(), comment = '份数')  # 份数
            F_BZZL = Column(FLOAT(18, 3), comment = '标准重量')  # 标准重量
            F_SJZL = Column(FLOAT(18, 3), comment = '实际重量')  # 实际重量
        # 样桶
        elif index == 2 and _type == "CY":
            F_YANGZHONG = Column(FLOAT(18, 3), comment = '样重')  # 样重
            F_SYTH = Column(String(8), comment = '收样桶号')  # 收样桶号
            F_SYTBH = Column(String(20), comment = '收样桶编号')  # 收样桶编号
            F_CYKSSJ = Column(DateTime(), comment = '采样开始时间')  # 采样开始时间
            F_CYJSSJ = Column(DateTime(), comment = '采样结束时间')  # 采样结束时间
            F_JYFS = Column(Integer(), comment = '集样方式')  # 集样方式

        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return table_sub


# 车箱注册表
def get_truck_info():
    class truck(base):
        __tablename__ = "T_330_BI_CLXX"
        F_ID = Column(String(36), comment = '车辆ID', primary_key = True)  # 车辆ID
        F_CHEHAO = Column(NVARCHAR(20), comment = '车号')  # 车号
        F_XCZH = Column(Integer(), comment = '行车证号')  # 行车证号
        F_SJMC = Column(String(20), comment = '司机名称')  # 司机名称
        F_YSDW = Column(Integer(), comment = '运输单位编码')  # 运输单位编码
        F_CLYT = Column(String(20), comment = '车辆用途')  # 车辆用途
        F_SFGY = Column(String(8), comment = '是否固用')  # 是否固用
        F_SPKH = Column(String(20), comment = '射频卡号')  # 射频卡号
        F_EDZZ = Column(DECIMAL(18, 3), comment = '额定载重')  # 额定载重
        F_KCZL = Column(DECIMAL(18, 3), comment = '空车重量')  # 空车重量
        F_CXSL = Column(Integer(), comment = '车厢数量')  # 车厢数量
        F_CXYCTJL = Column(DECIMAL(18, 3), comment = '车厢与车头距离')  # 车厢与车头距离
        F_LJSL = Column(Integer(), comment = '拉筋数量')  # 拉筋数量
        F_LJJJ = Column(String(20), comment = '拉筋间距')  # 拉筋间距
        F_CXC = Column(DECIMAL(18, 3), comment = '车厢长')  # 车厢长
        F_CXK = Column(DECIMAL(18, 3), comment = '车厢宽')  # 车厢宽
        F_CXG = Column(DECIMAL(18, 3), comment = '车厢高')  # 车厢高
        F_CXDG = Column(DECIMAL(18, 3), comment = '车厢底高')  # 车厢底高
        F_HCXYQCXJL = Column(DECIMAL(18, 3), comment = '后车厢与前车厢距离')  # 后车厢与前车厢距离
        F_HCXLJSL = Column(Integer(), comment = '后车厢拉筋数量')  # 后车厢拉筋数量
        F_HCXLJJJ = Column(String(20), comment = '后车厢拉筋间距')  # 后车厢拉筋间距
        F_HCXC = Column(DECIMAL(18, 3), comment = '后车厢长')  # 后车厢长
        F_HCXK = Column(DECIMAL(18, 3), comment = '后车厢宽')  # 后车厢宽
        F_HCXG = Column(DECIMAL(18, 3), comment = '后车厢高')  # 后车厢高
        F_HCXDG = Column(DECIMAL(18, 3), comment = '后车厢底高')  # 后车厢底高
        F_ZCSJ = Column(DateTime(), comment = '注册时间')  # 注册时间
        F_ZCRY = Column(String(20), comment = '注册人员')  # 注册人员
        F_SFTY = Column(Integer(), comment = '是否停用')  # 是否停用
        F_CZBZW = Column(Integer(), comment = '操作标志位')  # 操作标志位
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return truck


# 合样卸料主表
def get_unload_info(name = ''):
    class unload_main(base):
        __tablename__ = "T_540_{}UNLOAD_MASTE".format(name)
        F_ID = Column(String(32), comment = 'ID', primary_key = True)  # ID
        F_CZPZT = Column(String(20), comment = '状态')  # 状态
        F_KPSJ = Column(DateTime(), comment = '时间')  # 时间
        F_YCXX = Column(String(2000), comment = '异常提示')  # 异常提示
        F_CSSX = Column(Integer(), comment = '超时时限')  # 超时时限
        F_CYBM = Column(String(20), comment = '采样编码')  # 采样编码
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    # #合样卸料子表
    # def get_unload_sub(name = ''):
    class unload_sub(base):
        __tablename__ = "T_540_{}UNLOAD_DETAILONE".format(name)
        F_GID = Column(String(36), comment = '唯一标识', primary_key = True)  # 唯一标识
        F_PID = Column(String(36), comment = '主表标识')  # 主表标识
        F_SYTH = Column(String(20), comment = '收样桶号')  # 收样桶号
        F_TYBM = Column(String(50), comment = '收样桶编号')  # 收样桶编号
        F_FYBM = Column(String(50), comment = '封样编码')  # 封样编码
        F_YANGZHONG = Column(FLOAT(18, 3), comment = '样重')  # 样重
        F_HYSJ = Column(DateTime(), comment = '合样时间')  # 合样时间
        F_XLSJ = Column(DateTime(), comment = '卸料时间')  # 卸料时间
        F_ZHUANGTAI = Column(String(4), comment = '状态')  # 状态
        F_TIPINFO = Column(String(2000), comment = '提示信息')  # 提示信息
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return unload_main, unload_sub


def get_unload(name = ""):
    # 卸料命令表
    class unload_main_command(base):
        __tablename__ = "T_540_{}UNLOAD".format(name)
        ID = Column(String(36), comment = 'ID', primary_key = True)  # ID
        F_CYBM = Column(String(20), comment = '采样编码')  # 采样编码
        F_XYSJ = Column(String(20), comment = '发送时间')  # 发送时间
        DATASTATUS = Column(String(10), comment = '操作状态', server_default = '0')  # 操作状态
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    # 卸料结果表
    class unload_main_status(base):
        __tablename__ = "T_540_{}UNLOAD_STATUS".format(name)
        ID = Column(String(36), comment = 'ID', primary_key = True)  # ID
        F_CYBM = Column(String(20), comment = '采样编码')  # 采样编码
        F_XYSJ = Column(String(20), comment = '发送时间')  # 发送时间
        DATASTATUS = Column(String(10), comment = '操作状态', server_default = '0')  # 操作状态
        F_STATUS = Column(String(10), comment = '卸料状态', server_default = '0')  # 卸料状态
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return unload_main_command, unload_main_status


# 车号认别表
def get_chehao_info(name = ""):
    class chehao_table(base):
        __tablename__ = "T_540_{}CHEHAO".format(name) if name else "T_540_CHEHAO"
        F_ID = Column(String(36), comment = '车辆ID', primary_key = True)  # 车辆ID
        F_CHEHAO = Column(NVARCHAR(20), comment = '车号')  # 车号
        F_SBQBH = Column(String(20), comment = '识别器编号')  # 识别器编号
        F_CYJBH = Column(String(20), comment = '采样机编号')  # 采样机编号
        F_SBSJ = Column(DateTime(), comment = '车号识别时间')  # 车号识别时间
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return chehao_table


# 设备状态表
def get_rt_st(name):
    class rt_status(base):
        __tablename__ = "T_RT_ST" if not name else name
        F_ID = Column(String(36), comment = 'ID')  # ID
        F_PID = Column(String(36), comment = '父ID')  # 父ID
        F_NID = Column(int(2), comment = '整形ID')  # 整形ID
        F_MID = Column(int(2), comment = '点号')  # 点号
        F_CLASS = Column(String(36), comment = '类')  # 类
        F_URI = Column(String(36), comment = '设备唯一资源名')  # 设备唯一资源名
        F_TYPE = Column(int(2), comment = '类型')  # 类型
        F_V = Column(int(2), comment = '设备状态值')  # 设备状态值
        F_Q = Column(int(2), comment = '品质')  # 品质
        F_T = Column(int(), comment = '时标UTC')  # 时标UTC
        F_USER_CRT = Column(String(50), comment = '创建用户')  # 创建用户
        F_DT_CRT = Column(int(), comment = '创建时间UTC')  # 创建时间UTC
        F_USER_MOD = Column(String(50), comment = '修改用户')  # 修改用户
        F_DT_MOD = Column(int(), comment = '修改时间UTC')  # 修改时间UTC
        F_RES0 = Column(int(2), comment = '保留0')  # 保留0
        F_RES1 = Column(String(1024), comment = '保留1')  # 保留1
        F_SYN_FLAG = Column(Integer(), comment = '同步标志位', server_default = '0')  # 同步标记
        F_ADDTIME = Column(DateTime(), comment = '增加时间', nullable = False, server_default = func.now())  # 添加时间

    return rt_status


if __name__ == '__main__':
    print(111, func.datetime())